---@module mysqlinc

local _sql = class("MysqlInc")
-- ====================================================
--default params
local mysql_c = require("resty.mysql")
local d = Ding
local Logger = d.Logger
local UtilRds = d.UtilRds
--连接池的实际作用(管理类的连接池实现暂时不需要)
local IDLE_TIMEOUT = 10 * 1000 --10s
local KEEPALIVE_CNE = 10 --连接池的数量
local json = require("cjson")
-- ====================================================
_sql.dtype = "mysql"

function _sql:ctor()
    self._inc = mysql_c:new()
    self._iscon = false
end

--- Mysql数据库连接
-- 连接数据库，默认超时5000ms
-- @tparam   self 
-- @tparam  table cfg 数据库连接相关参数
-- @treturn boolean 返回连接courss
function _sql:connect(cfg)
    local ok, err = self._inc:connect(cfg)
    self._inc:set_timeout(5000)
    if ok then
        self._iscon = true
        return true
    else
        Logger:error("failed to connnect the mysql:%s", json.encode(cfg))
        return false
    end
end

function _sql:insert_data(tbname, ldic)
    if self._iscon and self._inc then
        local query_cmd = UtilRds.insert_data(tbname, ldic)
        return self:query_cmd(query_cmd)
    end
end

function _sql:find_all(tbname, condldic)
    if self._iscon and self._inc then
        local query_cmd = UtilRds.find(tbname, condldic)
        return self:query_cmd(query_cmd)
    end
end

function _sql:find_one(tbname, condldic)
    local list = self:find_all(tbname, condldic)
    if type(list) == "table" and  #list > 0 then 
        return list[1]
    end 
end

function _sql:find_in_set(tbname, key, vlist)
    local query_cmd = UtilRds.find_in_set(tbname, key, vlist)
    return self:query_cmd(query_cmd)
end

function _sql:find_in(tbname, key, vlist)
    local query_cmd = UtilRds.find_in(tbname, key, vlist)
    return self:query_cmd(query_cmd)
end

function _sql:dele_one(tbname, condldic)
    if self._iscon and self._inc then
        local query_cmd = UtilRds.dele_all(tbname, condldic)
        return self:query_cmd(query_cmd)
    end
end

function _sql:fetch_limitcnt(tbname, starnum, endnum)
    local query_cmd = UtilRds.fetch_limitcnt(tbname, starnum, endnum)
    return self:query_cmd(query_cmd)
end

function _sql:update(tbname, valdic, condic, ...)
    local query_cmd = UtilRds.update(tbname, valdic, condic, ...) 
    return self:query_cmd(query_cmd)
end

function _sql:release_conn()
    --todo keep-alive
    -- self._inc:close()
end

function _sql:transaction_cmd(query_cmds)
    if not self._inc or not self._inc then return end 
    ret = true
    ret = self:query_cmd("BEGIN")
    ret = self:query_cmd("SET AUTOCOMMIT=0")
    local ret = true
    for _, cmd in ipairs(query_cmds) do 
        if not type(cmds) == "string" then 
            Logger:error("[MYSQL queryfunc]transition values must query strings!")
            break
        end
        if (not self:query_cmd(cmd)) then 
            ret = false
            break
        end
    end
    if ret then 
        self:query_cmd("COMMIT")
    else 
        self:query_cmd("ROLLBACK")
    end 
    return ret 
end

function _sql:query_cmd(cmdstr)
    if self._iscon and self._inc then
        local res, err, errno, sqlstate  = self._inc:query(cmdstr)
        Logger:debug("mysql run cmd--%s", cmdstr)
        if not res then
            Logger:error("mysql query %s raise error:%s", query_cmd, err)
            return false
        end
        self:release_conn()
        return res, err, errno, sqlstate
    else 
        Logger:error("Query mysql failed!No connected yet!")
    end
end

function _sql:send_query(cmdstr)
    local ret, err = self._inc:send_query(cmdstr)
    return ret, err
end

function _sql:read_result()
    return self._inc:read_result()
end

return _sql
